﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using System.Net.Sockets;
using sdkMulticastCS;
using System.Windows.Threading;
using System.Text;
using Microsoft.Phone.Shell;

namespace WindowsPhoneServer
{
    public partial class MainPage : PhoneApplicationPage
    {
        GeoCoordinateWatcher watcher { get; set; }
        UdpAnySourceMulticastChannel channel { get; set; }
        DispatcherTimer timer { get; set; }
        private const string GROUP_ADDRESS = "224.0.1.11";
        private const int GROUP_PORT = 1125;


        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            this.onr
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            channel = new UdpAnySourceMulticastChannel(GROUP_ADDRESS, GROUP_PORT);
            channel.Open();
            // The watcher variable was previously declared as type GeoCoordinateWatcher. 
            if (watcher == null)
            {
                watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); // using high accuracy
                watcher.MovementThreshold = 20; // use MovementThreshold to ignore noise in the signal
                watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
                watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
                watcher.Start();
            }

            if (null == timer)
            {
                timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromMilliseconds(1000);
                timer.Tick += new EventHandler(timer_Tick);
                timer.Start();
            }
            Microsoft.Phone.Shell.PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;

        }

        void timer_Tick(object sender, EventArgs e)
        {
            GeoCoordinate gc = watcher.Position.Location;
            if(!gc.IsUnknown)
            {
                string strEnd = "\r\n";
                string rmc = GeoConvertor.ToRMC(gc);
                channel.SendTo(new IPEndPoint(IPAddress.Parse(GROUP_ADDRESS), GROUP_PORT), "{0}{1}", rmc, strEnd);
                string gga = GeoConvertor.ToGGA(gc);
                channel.SendTo(new IPEndPoint(IPAddress.Parse(GROUP_ADDRESS), GROUP_PORT), "{0}{1}", gga, strEnd);
                channel.SendTo(new IPEndPoint(IPAddress.Parse(GROUP_ADDRESS), GROUP_PORT), "{0}{1}", "$GPGSV,2,1,05,22,60,222,40,16,35,258,34,03,33,310,30,29,12,138,36*73", strEnd);
                channel.SendTo(new IPEndPoint(IPAddress.Parse(GROUP_ADDRESS), GROUP_PORT), "{0}{1}", "$GPGSV,2,2,05,14,04,174,41,,,,,,,,,,,,*4A", strEnd);
                channel.SendTo(new IPEndPoint(IPAddress.Parse(GROUP_ADDRESS), GROUP_PORT), "{0}{1}", "$GPGSA,A,3,03,14,16,22,29,,,,,,,,1.1,0.5,1.0*3C", strEnd);
              
            }
                        
        }

        void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            txtLat.Text = e.Position.Location.Latitude.ToString();
            txtLon.Text = e.Position.Location.Longitude.ToString();
            txtAlt.Text = e.Position.Location.Altitude.ToString();
            //channel.SendTo(new IPEndPoint(IPAddress.Parse(GROUP_ADDRESS), 1125), "{0}:{1}:{2}", e.Position.Location.Latitude, e.Position.Location.Longitude, e.Position.Location.Altitude);
            string rmc = GeoConvertor.ToRMC(e.Position.Location);
            channel.SendTo(new IPEndPoint(IPAddress.Parse(GROUP_ADDRESS), 1125), "{0}", rmc);
        }

        void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            txtTest.Text = e.Status.ToString();
            switch (e.Status)
            {
                case GeoPositionStatus.Disabled:
                    break;
                case GeoPositionStatus.Initializing:
                    break;
                case GeoPositionStatus.NoData:
                    break;
                case GeoPositionStatus.Ready:
                    break;
                default:
                    break;
            }
        }

        private void bntStop_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Phone.Shell.PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;
            watcher.Stop();
            watcher = null;
            channel.Close();
            channel = null;
            timer.Stop();
            timer = null;
            txtTest.Text = "stopped";
        }
    }
}